﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;


namespace LaneLpnr.H264
{
    public class H264Decoder
    {

        ///初始化摄像机   *   strIP - 识别机的IP地址 (IPv4) *   Track - 获取哪一个码流（0：主码流，1：次码流）
        [DllImport("H264Decoder.dll", EntryPoint = "H264DEC_Init", SetLastError = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public static extern IntPtr H264DEC_Init(string ip, int track);

        //释放所有资源，包括句柄本身
        [DllImport("H264Decoder.dll", EntryPoint = "H264DEC_Terminate", SetLastError = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public static extern bool H264DEC_Terminate(IntPtr h);

        //暂停接收
        [DllImport("H264Decoder.dll", EntryPoint = "H264DEC_Suspend", SetLastError = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public static extern bool H264DEC_Suspend(IntPtr h);

        //恢复接收：重新建立连接开始接收
        [DllImport("H264Decoder.dll", EntryPoint = "H264DEC_Resume", SetLastError = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public static extern bool H264DEC_Resume(IntPtr h);

        //消息接收窗口(Windows)
        [DllImport("H264Decoder.dll", EntryPoint = "H264DEC_SetWinMsg", SetLastError = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public static extern bool H264DEC_SetWinMsg(IntPtr h, IntPtr hwnd, int msgno);// h mable is LPNR_Init back intptr ， hwnd mable is window  intptr

        //回调
        [DllImport("LPNR.dll", EntryPoint = "H264DEC_SetCallBack", SetLastError = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public static extern bool H264DEC_SetCallBack(IntPtr h, H264DEC_callback mycbfxc);
        public delegate void H264DEC_callback(IntPtr h, int a); //声明委托  


        //如果不确定需要多大的buffer， 可以调用‘H264DEC_GetFrameSize’
        [DllImport("H264Decoder.dll", EntryPoint = "H264DEC_GetFrameImage", SetLastError = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public static extern long H264DEC_GetFrameImage(IntPtr h, byte[] buf);


        [DllImport("H264Decoder.dll", EntryPoint = "H264DEC_GetFrameSize", SetLastError = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public static extern long H264DEC_GetFrameSize(IntPtr h);

        //获取视频的解像度，单位是像素。（返回TRUE成功，在第一帧解码完后才会有这个数字）
        [DllImport("H264Decoder.dll", EntryPoint = "H264DEC_GetFrameResolution", SetLastError = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public static extern bool H264DEC_GetFrameResolution(IntPtr h, int width, int height);

        //返回视频流的码率（计算最近20帧的值），单位是bits-per-second。除以1024是 kbps
        [DllImport("H264Decoder.dll", EntryPoint = "H264DEC_GetDataRate", SetLastError = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public static extern double H264DEC_GetDataRate(IntPtr h);

        //返回视频流的帧率，单位是frames-per-second。一样用最近20帧的时间来计算
        [DllImport("H264Decoder.dll", EntryPoint = "H264DEC_GetFrameSpeed", SetLastError = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public static extern double H264DEC_GetFrameSpeed(IntPtr h);

        //返回创建对象到当前已经接收解码的视频帧数。
        [DllImport("H264Decoder.dll", EntryPoint = "H264DEC_GetFrameCount", SetLastError = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public static extern long H264DEC_GetFrameCount(IntPtr h);

        //当前是否连线。（返回TRUE：yes，FALSE：no）
        [DllImport("H264Decoder.dll", EntryPoint = "H264DEC_IsVideoOnline", SetLastError = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public static extern bool H264DEC_IsVideoOnline(IntPtr h);

        //开启视频
        [DllImport("H264Decoder.dll", EntryPoint = "H264DEC_StartVideo", SetLastError = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public static extern bool H264DEC_StartVideo(IntPtr h, int enable);



    }
}
